﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Wuther.Util.Helper;
using Wuther.Util.Models;
using Microsoft.Extensions.Configuration;
using Microsoft.AspNetCore.Authorization;
using System.Net.Http;
using System.Net;
using IdentityModel.Client;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;

namespace Wuther.Api.Controllers
{
    [Route("api/[controller]"), ApiExplorerSettings(GroupName = "UploadFile")]
    [ApiController]
    public class UploadFileController : BaseController
    {
        private readonly IConfiguration _configuration;
        public UploadFileController(IConfiguration configuration)
        {
            _configuration = configuration;
        }
        [HttpGet]
        public IActionResult GetFile()
        {
            return new JsonResult("成功");
        }

        [Authorize]
        [HttpPost(Name = nameof(UploadPicture))]
        public async Task<IActionResult> UploadPicture()
        {
            var files = HttpContext.Request.Form.Files;
            ImgResult result = new ImgResult();
            IList<ImgInfo> imgInfos = new List<ImgInfo>();
            var token = HttpContext.Request.Headers["Authorization"].ToString();
            var apiClient = new HttpClient();
            apiClient.SetBearerToken(token.Remove(0,6));
            var response = await apiClient.GetAsync("http://47.119.127.197:6002/connect/userinfo");
            if (!response.IsSuccessStatusCode)
            {
                result.Errno = (int)response.StatusCode;
                result.Data = imgInfos;
                return new JsonResult(result);
            }
            else
            {
                var content = await response.Content.ReadAsStringAsync();
                var userInfo = JsonConvert.DeserializeObject<IdentityUserInfo>(content);

                var path = _configuration.GetSection("UiPath").GetSection("BlogPath").Value;
                var filePath = Path.Combine(path, userInfo.preferred_username, "img");
                if (!FileHelper.IsExistDirectory(filePath))
                {
                    FileHelper.CreateDirectory(filePath);
                }
                foreach (IFormFile file in files)
                {
                    ImgInfo img = new ImgInfo();
                    img.Url = $"\\{userInfo.preferred_username}\\img\\{file.FileName}";
                    img.alt = "";
                    img.href = $"\\{userInfo.preferred_username}\\img\\{file.FileName}";
                    var filename = Path.Combine(filePath, $"{file.FileName}");
                    //判断是否有重复图片
                    using (FileStream stream = System.IO.File.Create(filename))
                    {
                        file.CopyTo(stream);
                        stream.Position = 0;
                        var fileName = file.FileName;
                        stream.Flush();
                    }
                    imgInfos.Add(img);
                }
                result.Errno = 0;
                result.Data = imgInfos;
                return new JsonResult(result);

            }




            //foreach (IFormFile file in files)
            //{
            //    ImgInfo img = new ImgInfo();
            //    img.Url = $"\\admin\\img\\{file.FileName}";
            //    img.alt = "";
            //    img.href = $"\\admin\\img\\{file.FileName}";
            //    //判断是否有重复图片
            //    using (FileStream stream = System.IO.File.Create($"D:\\Project4\\wuther.ui.vue3\\public\\admin\\img\\{file.FileName}"))
            //    {
            //        file.CopyTo(stream);
            //        stream.Position = 0;
            //        var fileName = file.FileName;
            //        stream.Flush();
            //    }
            //    imgInfos.Add(img);
            //}
            //result.Errno = 0;
            //result.Data = imgInfos;
            //return new JsonResult(result);
        }
    }
}
